//
//  STCommentMagVC.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/11.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STCommentMagVC.h"
#import "STCommentsListView.h"
#import "YLNavTitleBigSmallLab.h"

@interface STCommentMagVC ()<STCommentsListViewDelegate>{
    NSMutableArray<STCommentInfoModel> * arrComments;
    STDoorCommonModel * commentInfo;
    STCommentsListView * commentListView;
    UILabel * titleLab;
    int currentPage;
}

@end

@implementation STCommentMagVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    titleLab = [YLNavTitleBigSmallLab getNavigationBigAndSmallTitleWithString:@"评价管理" smallFromLocation:4];
    self.navigationItem.titleView = titleLab;
    
    [self wr_setNavBarTintColor:[UIColor blackColor]];
    
    currentPage = 0;
    
    [self loadAllCommentListView];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    // Do any additional setup after loading the view.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self saveDataToSandBox];
}

- (void)loadAllCommentListData {
    
    [STMainPageService getDoorTotalScoreWithstylistId:1 completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        commentInfo = [STDoorCommonModel modelWithJSON:result];
        commentListView.totalScore =commentInfo;
        [commentListView reloadTotalCell];
        NSString * titleStr = [NSString stringWithFormat:@"评价管理(%ld)",commentInfo.scoreCount];
        titleLab = [YLNavTitleBigSmallLab getNavigationBigAndSmallTitleWithString:titleStr smallFromLocation:4];
        self.navigationItem.titleView = titleLab;
    }];
    
    [STMainPageService getDoorCommentWithstylistId:1 page:currentPage size:20 completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }else{
            NSArray * arrModels = [NSArray modelArrayWithClass:[STCommentInfoModel class] json:result];
            if (currentPage ==0) {
                arrComments = (NSMutableArray<STCommentInfoModel> *)[NSMutableArray arrayWithArray:arrModels];
            }else{
                [arrComments addObjectsFromArray:arrModels];
            }
            commentListView.oldCommentData = arrComments;
            [commentListView reloadCommentListView];
        }
    }];
    
    
    
}

- (void)loadAllCommentListView {
    //
    //    NSArray * arr = [ZYHCommonMethod getAutoModelToSandBoxWithClassName:@"NSArray" withPath:@"comments.txt"];
    //    NSArray * arrModels = [NSArray modelArrayWithClass:[STCommentInfoModel class] json:arr];
    //
    //    commentInfo = [ZYHCommonMethod getAutoModelToSandBoxWithClassName:@"STDoorCommonModel" withPath:@"commentInfo.txt"];
    
    commentListView = [[STCommentsListView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //    commentListView.oldCommentData =(NSMutableArray<STCommentInfoModel> *)[NSMutableArray arrayWithArray:arrModels];
    commentListView.delegate = self;
    commentListView.totalScore = commentInfo;
    [commentListView setCommentsListView];
    [self.view addSubview:commentListView];
    [self loadAllCommentListData];
    
}

//界面代理
//下拉刷新
-(void)commentsListUpdataNew{
    currentPage =0;
    [self loadAllCommentListData];
    
}

//上划加载
-(void)commentsListAddMore{
    currentPage ++;
    [self loadAllCommentListData];
}



//选择的评论
-(void)commentsListViewSelNum:(int)selNum{
    NSLog(@"跳转评论详情");
}

//回复评论
-(void)commentsListView:(STCommentsListView *)view indexpath:(NSIndexPath *)indexPath replayCommentText:(NSString *)replayStr{
    STCommentInfoModel * model = arrComments[indexPath.row];
    [STMainPageService replayDoorCommentWithCommentId:model.objectId context:replayStr completion:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }else{
            [ZYHCommonMethod showSuccess:@"回复成功"];
        }
    }];
}

//选择某一评论的图片
-(void)commentsListView:(STCommentsListView *)view indexpath:(NSIndexPath *)indexPath selImgNum:(int)selNum{
    NSLog(@"显示图片");
}

-(void)saveDataToSandBox{
    if(arrComments){
        [ZYHCommonMethod saveAutoModelToSandBox:arrComments withPath:@"comments.txt"];
    }
    if(commentInfo){
        [ZYHCommonMethod saveAutoModelToSandBox:commentInfo withPath:@"commentInfo.txt"];
    }
}

@end
